﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Author: AustralTek 
/// Create date: 2010/05/04
/// http://www.australtek.com
/// Copyright: 2009 - 2010
/// </summary>

#region "ADO.NET DataSet to JSON"
public static class DataSet2JSON
{
    
    public static object RowsToDictionary(this DataTable table)
    {
        var columns = table.Columns.Cast<DataColumn>().ToArray();
        return table.Rows.Cast<DataRow>().Select(r => columns.ToDictionary(c => c.ColumnName, c => r[c]));
    }

    public static Dictionary<string, object> ToDictionary(this DataTable table)
    {
        return new Dictionary<string, object>
       {
           { table.TableName, table.RowsToDictionary() }
       };
    }

    public static Dictionary<string, object> ToDictionary(this DataSet data)
    {
        return data.Tables.Cast<DataTable>().ToDictionary(t => t.TableName, t => t.RowsToDictionary());
    }

}
#endregion
